local snow    = require("meet_your_mods.mhr").snow
local status  = require("meet_your_mods.status")
local routine = require("meet_your_mods.routine_framework")
local setting = require("meet_your_mods.setting")
local ui      = require("meet_your_mods.ui")

local myset_bot = {
    setting = {
        apply_on_return = true, --返回时补充道具
        apply_on_tent   = true, --进入帐篷时补充道具

        last_item_set = {},
    },
    message = {
        [status.languages.japanese]            = { "マイセット", "一部のアイテムが不足している", },
        [status.languages.english]             = { "Item loadout", "Some items can't be fully restocked.", },
        [status.languages.chinese_troditional] = { "道具组合", "部分道具不足", },
        [status.languages.chinese_simplified]  = { "道具组合", "部分道具不足", }
    },
}

--应用道具组合
function myset_bot:apply_item_set(myset_id, apply_shortcut)
    if not myset_id or myset_id < 0 then
        return
    end

    local myset = snow.data.DataManager:get_singleton():get_ItemMySet():getData(myset_id)
    if not myset or not myset:isUsing() then
        self.setting.last_item_set[status:current_save_data()] = nil
        setting:save_setting()
        return sdk.PreHookResult.CALL_ORIGINAL
    end

    --应用道具组合
    snow.data.DataManager:get_singleton():get_ItemMySet():applyItemMySet(myset_id)

    --应用快捷栏组合
    local palette_set = myset:get_PaletteSetIndex()
    if apply_shortcut and palette_set:get_HasValue() then
        snow.data.SystemDataManager:get_singleton():getCustomShortcutSystem()
            :setUsingPaletteIndex(snow.data.CustomShortcutSystem.SycleTypes.Quest, palette_set:get_Value())
    end

    --提示道具不足
    if not myset:isEnoughItem() then
        local messages = self.message[status:display_language()] or self.message[status.languages.default]
        snow.gui.ChatManager:get_singleton():reqAddChatInfomation(messages[1]
            .. "\n　<COL YEL>" .. myset:get_Name() .. "</COL>"
            .. "\n" .. messages[2], 2289944406)
    end
end

--返回时应用道具组合
function myset_bot:pre_return_village(_)
    if self.setting.apply_on_return then
        self:apply_item_set(self.setting.last_item_set[status:current_save_data()])
    end

    return sdk.PreHookResult.CALL_ORIGINAL
end

--保存上次应用的道具组合
function myset_bot:on_apply_item_set(index)
    if self.setting.apply_on_return or self.setting.apply_on_tent then
        self.setting.last_item_set[status:current_save_data()] = index
        setting:save_setting()
    end
end

--进入帐篷时补充道具
function myset_bot:on_in_tent()
    if self.setting.apply_on_tent then
        self:apply_item_set(self.setting.last_item_set[status:current_save_data()])
    end
    return sdk.PreHookResult.CALL_ORIGINAL
end

function myset_bot:initialize()
    --记录最近一次应用的道具组合
    snow.data.ItemMySet.applyItemMySet:sdk_hook(function(parameters)
        self:on_apply_item_set(sdk.to_int64(parameters[3]))
    end)

    --返回村庄时应用最近一次应用过的道具组合
    routine:hook(self)

    --进入帐篷时应用最近一次应用过的道具组合
    snow.stage.StageManager.setTentFlag:sdk_hook(function(parameters)
        if 0 == sdk.to_int64(parameters[3]) then self:on_in_tent() end
    end)

    setting:bind_setting("myset", self.setting)

    ui:cluster("autobot", "自动功能")
    ui:static_text("autobot", "个人组合增强")
    ui:checkbox("autobot", "myset", "apply_on_return", "返回时补充道具", nil, true)
    ui:checkbox("autobot", "myset", "apply_on_tent", "进入帐篷时补充道具", nil, true)
end

function myset_bot:normalize()
    local item_sets = {}
    for player, item in pairs(self.setting.last_item_set) do
        item_sets[tonumber(player)] = item
    end
    self.setting.last_item_set = item_sets
end

return myset_bot
